package com.xxgc.eow.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.github.pagehelper.PageHelper;
import com.xxgc.eow.entity.Article;
import com.xxgc.eow.entity.Articletitle;
import com.xxgc.eow.mapper.ArticleMapper;
import com.xxgc.eow.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 发表文章 服务实现类
 * </p>
 *
 * @author Mr.Abel
 * @since 2023-05-21
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public List<Article> selectAll() {
        return articleMapper.selectAllArticle();
    }

    @Override
    public List<Article> selectPage(int PageNum,int PageSize) {
        PageHelper.startPage(PageNum,PageSize);
        return articleMapper.selectAllArticle();
    }

    @Override
    public List<Article> selectTitleName(int articleId) {
        return articleMapper.selectTitleName(articleId);
    }

    @Override
    public int insertArticle(Article article) {
        System.out.println(article);
        return articleMapper.insertArticle(article);
    }

    @Override
    public Integer selectTitleId(String titleName) {
        return articleMapper.selectTitleIdByTitleName(titleName);
    }

    @Override
    public List<Article> selectKikeTitle(Article article,
                                         int PageSize,
                                         int PageNum) {
        if (PageNum == 0){
            return articleMapper.selectLIKEArticle(article);
        }else{
            PageHelper.startPage(PageNum,PageSize);
            return articleMapper.selectLIKEArticle(article);
        }


    }

    @Override
    public int delArticle(int articleId) {
        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return articleMapper.delArticle(articleId,localDateTime);
    }

    @Override
    public Integer selectArticleId(Article article) {
        return articleMapper.selectArticleId(article);
    }

    @Override
    public int insertArticleTitle(Articletitle articletitle) {
        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        articletitle.setCreateTime(localDateTime);
        return articleMapper.insertArticleTitle(articletitle);
    }


    @Override
    public int updateArticleAll(Article article) {
        return articleMapper.updateArticle(article);
    }

    @Override
    public int delArticleTitle(int articleId) {
        return articleMapper.delTitleArticle(articleId);
    }

    @Override
    public List<Article> selectByUserId(int userId) {
        return articleMapper.selectBuUserId(userId);
    }
}
